package com.volkanic.buttons;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;

/**
 * Created by Lucas on 20/07/2014.
 */
public class Button {

    private MotionTextButton button;

    /**
     * Constructor
     * @param text
     * @param current
     * @param last
     * @param speed
     */
    public Button(String text, Vector2 current, Vector2 last, float speed){
        float width = Gdx.graphics.getWidth() / 2f;
        float height = Gdx.graphics.getHeight() / 10f;

        // Set up the font of the button
        BitmapFont font = this.setUpFont(2);
        Skin skin = this.setUpSkin();
        TextButtonStyle style = this.setUpButtonStyle(font, skin);
        button = new MotionTextButton(text, style, current, last, speed, width);

        // Size and position
        this.setSize(width, height);

    }

    /**
     * Set up the textures of the button
     */
    private Skin setUpSkin(){
        // Initialise button
        Skin skin = new Skin();
        TextureAtlas buttonAtlas = new TextureAtlas(Gdx.files.internal("wooden-button.atlas"));
        skin.addRegions(buttonAtlas);
        return skin;
    }

    /**
     * Set up the font use inside the textButton
     * @param scale the scale of the text style in the button
     * @return the new font used for the button
     */
    private BitmapFont setUpFont(float scale){
        BitmapFont font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
        font.setScale(scale);
        return font;
    }

    /**
     * Set up the different texture for each state of the button
     * @param font the font used to create the style of the button
     * @return the new TextButtonStyle
     */
    private TextButtonStyle setUpButtonStyle(BitmapFont font, Skin skin){
        TextButtonStyle buttonStyle = new TextButtonStyle();
        buttonStyle.up = skin.getDrawable("wood");
        buttonStyle.down = skin.getDrawable("wood-pressed");
        buttonStyle.over = skin.getDrawable("wood-pressed");
        buttonStyle.font = font;
        return buttonStyle;
    }

    /**
     * Set the size of the button
     * @param width the new width of the button
     * @param height the new size of the button
     */
    public void setSize(float width, float height){
        button.setSize(width, height);
    }

    /**
     * @return the button
     */
    public TextButton getButton(){
        return this.button;
    }
}
